package world.criver.h50529.function;

import java.nio.charset.StandardCharsets;

public class funMessageFormat {
    /**
     * 字符串转16进制
     * @param str
     * @return
     */
    public String stringToHex(String str){
        byte[] bs=str.getBytes(StandardCharsets.UTF_8);
        StringBuilder me= new StringBuilder();
        for(byte i:bs) me.append(Integer.toHexString(i).replace("ffffff",""));
        return me.toString().toUpperCase();
    }

    /**
     * 十六进制转字符串
     * @param hex
     * @return
     */
    public String hexToString(String hex){
        hex=hex.toUpperCase().replaceAll(" ","");
        byte[] bs=new byte[hex.length()/2];
        for(int i=0;i<bs.length;i++)bs[i]= (byte) Integer.parseInt(hex.substring(i*2,i*2+2),16);
        return new String(bs,StandardCharsets.UTF_8);
    }

    /**
     * 十六进制字符串转byte[]
     * @param hex
     * @return
     */
    public byte[] hexToByte(String hex){
        hex=hex.toUpperCase().replaceAll(" ","");
        byte[] bs=new byte[hex.length()/2];
        for(int i=0;i<bs.length;i++)bs[i]=(byte) Integer.parseInt(hex.substring(i*2,i*2+2),16);
        return bs;
    }
}
